﻿using DFGK.WMS.Common.Result;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using DFGK_WMS.Repository;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Mvc;

namespace DFGJ.WMS_PDA.WebAPI.Controllers.PDA
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PDAStocktakingController : Controller
    {
        private IPDAStocktakingRepository repository;
        public PDAStocktakingController(IPDAStocktakingRepository repository)
        {
            this.repository = repository;
        }


        /// <summary>
        /// 获取所有盘点单号
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllStocktaking()
        {
            try
            {
                var list = repository.GetAllStocktaking();
                if (list != null && list.Count != 0)
                {
                    return Ok(BaseResult.Result(list, "查询成功", "200"));
                }
                return Ok(BaseResult.Result(null, "查询失败", "500"));
            }
            catch (Exception ex)
            {
                return Ok(BaseResult.Result(null, "查询失败", "500"));
            }
        }

        /// <summary>
        /// 根据OrderSn获取盘点单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStocktakingOrdersByOrderSn(string OrderSn)
        {
            try
            {
                var list = repository.GetStocktakingOrdersByOrderSn(OrderSn);
                if (list != null && list.Count != 0)
                {
                    return Ok(BaseResult.Result(list, "查询成功", "200"));
                }
                return Ok(BaseResult.Result(null, "查询失败", "500"));
            }
            catch (Exception ex)
            {
                return Ok(BaseResult.Result(null, "查询失败", "500"));
            }
        }

        /// <summary>
        /// 盘点结果记录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddStocktakingResult(TStocktakeResultRecorddtofengzhuang dto)
        {
            string message;
            ApiItemsResult apiItemsResult = new ApiItemsResult();
            bool fanhuishuju = repository.AddStocktakingResult(dto, out message);

            if (!fanhuishuju)
            {
                apiItemsResult.code = "500";
                apiItemsResult.message = message;
                apiItemsResult.data = fanhuishuju;
                return Ok(apiItemsResult);
            }
            apiItemsResult.code = "200";
            apiItemsResult.message = message;
            apiItemsResult.data = fanhuishuju;
            return Ok(apiItemsResult);
        }

    }
}
